<?php
namespace Admin\Controller;
use Common\Controller\AdminBaseController;

class ChatController extends AdminBaseController {

	
	public function index(){
		$this->display();
	}

	public function add(){
		M('chat')->add(I('post.'));
		
	}

	public function query(){

		$count = I('count');		
		$s=0;
		while(true){
			sleep(1);
			$s++;
			if($s==20){
				$data = array(
					'success'=>false,
					'count'=>$count,	
				);
				$this->ajaxReturn($data);

				exit;
			}
			
			if($count<M('chat')->count()){
				$res = M('chat')->select();
				$data = array(
					'success'=>true,
					'count'=>M('chat')->count(),
					'rows'=>$res		
				);
				$this->ajaxReturn($data);
			}			
		
		}
		
	
	}



	public function read(){
		$count=I('count');
		$s = 0;
		while(true){

			usleep(500000);
			$s++;
			if($s>=10){
				echo '{"success":false}';
				exit();
			}else{
				
				if($count<M('chat')->count()){
					$count=M('chat')->count();
					$start = $count-10;
					if ($count<10){
						$start = 0;	
					}else{
						$start = $count-10;
					}
					$data = M('chat')->limit("$start , 10")->select();				
					$res = array(
						'success'=>true,
						'content'=>$data,
						'count'=>$count,
					);			
					
					$this->ajaxReturn($res);
					exit();
				}
			}			
		
		}
		
	}
}

